package com.ziyun.erp.web.modules.promotion.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ziyun.erp.modules.promotion.entity.UserCouponExchangeEntity;
import com.ziyun.erp.modules.promotion.service.UserCouponExchangeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 用户优惠券兑换表
 *
 * @author jiangli
 * @email 
 * @date 2018-11-30 14:44:29
 */
@RestController
@RequestMapping("promotion/usercouponexchange")
public class UserCouponExchangeController {
    @Autowired
    private UserCouponExchangeService userCouponExchangeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("promotion:usercouponexchange:list")
    public R list(@RequestBody Map<String, Object> params){
        PageUtils page = userCouponExchangeService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 生成优惠券兑换码
     */
    @RequestMapping("/createCode")
    public R createCode(@RequestBody Map<String, Object> params){
        userCouponExchangeService.createCode(params);

        return R.ok();
    }

    /**
     * 批量删除
     */
    @RequestMapping("/deletes")
    public R deletes(@RequestBody List<UserCouponExchangeEntity> list){
        for (UserCouponExchangeEntity userCouponExchangeEntity : list) {
            userCouponExchangeEntity.setDisabled(true);
        }
        userCouponExchangeService.updateBatchById(list);

        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("promotion:usercouponexchange:info")
    public R info(@PathVariable("id") Long id){
        UserCouponExchangeEntity userCouponExchange = userCouponExchangeService.selectById(id);

        return R.ok().put("userCouponExchange", userCouponExchange);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("promotion:usercouponexchange:save")
    public R save(@RequestBody UserCouponExchangeEntity userCouponExchange){
        userCouponExchangeService.insert(userCouponExchange);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("promotion:usercouponexchange:update")
    public R update(@RequestBody UserCouponExchangeEntity userCouponExchange){
        ValidatorUtils.validateEntity(userCouponExchange);
        userCouponExchangeService.updateAllColumnById(userCouponExchange);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("promotion:usercouponexchange:delete")
    public R delete(@RequestBody Long[] ids){
        userCouponExchangeService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
